package common;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Order {
    /**
     * 订单编号
     */
    private String orderNumber;
    
    /**
     * 订单日期
     */
    private Date orderDate;
    
    /**
     * 订单总金额
     */
    private double totalAmount;
    
    /**
     * 订单项列表
     */
    private List<OrderItem> orderItems;
    
    /**
     * 客户编号
     */
    private int customerId;

    public Order() {
        this.orderItems = new ArrayList<>();
        this.orderDate = new Date();
        this.totalAmount = 0.0;
        this.customerId = 0;
    }

    public Order(String orderNumber, int customerId) {
        this.orderNumber = orderNumber;
        this.orderItems = new ArrayList<>();
        this.orderDate = new Date();
        this.totalAmount = 0.0;
        this.customerId = customerId;
    }

    // 添加订单项
    public void addOrderItem(Product product, int quantity) {
        OrderItem item = new OrderItem(product, quantity);
        orderItems.add(item);
        calculateTotalAmount();
    }

    // 移除订单项
    public void removeOrderItem(Product product) {
        orderItems.removeIf(item -> item.getProduct().getId() == product.getId());
        calculateTotalAmount();
    }

    // 更新订单项数量
    public void updateOrderItemQuantity(Product product, int quantity) {
        for (OrderItem item : orderItems) {
            if (item.getProduct().getId() == product.getId()) {
                item.setQuantity(quantity);
                calculateTotalAmount();
                break;
            }
        }
    }

    // 计算订单总金额
    private void calculateTotalAmount() {
        totalAmount = 0;
        for (OrderItem item : orderItems) {
            totalAmount += item.getSubtotal();
        }
    }

    // Getters and Setters
    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    public double getTotalAmount() {
        return totalAmount;
    }

    public List<OrderItem> getOrderItems() {
        return new ArrayList<>(orderItems); // 返回副本以防止外部修改
    }

    // Getters and Setters for customerId
    public int getCustomerId() {
        return customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }

    @Override
    public String toString() {
        return "common.Order{" +
                "orderNumber='" + orderNumber + '\'' +
                ", orderDate=" + orderDate +
                ", customerId=" + customerId +
                ", totalAmount=" + totalAmount +
                ", itemCount=" + orderItems.size() +
                '}';
    }

    // 订单项内部类
    public static class OrderItem {
        /**
         * 商品信息
         */
        private Product product;
        
        /**
         * 商品数量
         */
        private int quantity;
        
        /**
         * 商品单价
         */
        private double unitPrice;

        public OrderItem(Product product, int quantity) {
            this.product = product;
            this.quantity = quantity;
            this.unitPrice = product.getUnitPrice();
        }

        // 计算订单项小计
        public double getSubtotal() {
            return unitPrice * quantity;
        }

        // Getters and Setters
        public Product getProduct() {
            return product;
        }

        public void setProduct(Product product) {
            this.product = product;
            this.unitPrice = product.getUnitPrice();
        }

        public int getQuantity() {
            return quantity;
        }

        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }

        public double getUnitPrice() {
            return unitPrice;
        }

        public void setUnitPrice(double unitPrice) {
            this.unitPrice = unitPrice;
        }

        @Override
        public String toString() {
            return "OrderItem{" +
                    "product='" + product.getName() + '\'' +
                    ", quantity=" + quantity +
                    ", unitPrice=" + unitPrice +
                    ", subtotal=" + getSubtotal() +
                    '}';
        }
    }
}